#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=1e6+5;

vector<ll> a[N];
priority_queue<ll,vector<ll>,greater<ll>> q;

void solve(){
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++){
        ll v,s;
        cin>>v>>s;
        a[s].push_back(v);
    }
    ll ans=0,sum=0;
    for(ll i=n;i>=1;i--){
        for(auto u:a[i]){
            sum+=u;
            q.push(u);
        }
        while(q.size()>i){
            sum-=q.top();
            q.pop();
        }
        ans=max(ans,sum);
    }
    cout<<ans<<endl;

    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}